/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.nuo.backend.modules.shiro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuo.backend.common.utils.R;
import com.nuo.backend.modules.shiro.entity.SysUserTokenEntity;
import com.nuo.backend.modules.shiro.oauth2.Oauth2Yml;
import com.nuo.backend.modules.shiro.oauth2.TokenGenerator;
import com.nuo.backend.modules.shiro.dao.SysUserTokenDao;
import com.nuo.backend.modules.shiro.service.SysUserTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service("sysUserTokenService")
public class SysUserTokenServiceImpl extends ServiceImpl<SysUserTokenDao, SysUserTokenEntity> implements SysUserTokenService {

	@Autowired
	private Oauth2Yml oauth2Yml;

	@Override
	public R createToken(long userId) {
		//生成一个token
		String token = TokenGenerator.generateValue();

		//当前时间
		Date now = new Date();
		//过期时间
		Date expireTime = new Date(now.getTime() + oauth2Yml.getExpire() * 1000);

		//判断是否生成过token
		SysUserTokenEntity tokenEntity = this.getOne(new LambdaQueryWrapper<SysUserTokenEntity>()
				.eq(SysUserTokenEntity::getUserId, userId)
				.isNull(SysUserTokenEntity::getIdentity)
		);
		if(tokenEntity == null){
			tokenEntity = new SysUserTokenEntity();
			tokenEntity.setUserId(userId);
			tokenEntity.setToken(token);
			tokenEntity.setUpdateTime(now);
			tokenEntity.setExpireTime(expireTime);

			//保存token
			this.save(tokenEntity);
		}else{
			tokenEntity.setToken(token);
			tokenEntity.setUpdateTime(now);
			tokenEntity.setExpireTime(expireTime);

			//更新token
			this.update(new LambdaUpdateWrapper<SysUserTokenEntity>()
					.eq(SysUserTokenEntity::getUserId, userId)
					.isNull(SysUserTokenEntity::getIdentity)
					.set(SysUserTokenEntity::getToken, token)
					.set(SysUserTokenEntity::getUpdateTime, now)
					.set(SysUserTokenEntity::getExpireTime, expireTime)
			);
		}

		R r = R.ok().put("token", token).put("expire", oauth2Yml.getExpire());
		return r;
	}


	@Override
	public R createToken(long userId,Integer identity) {
		//生成一个token
		String token = TokenGenerator.generateValue();

		//当前时间
		Date now = new Date();
		//过期时间
		Date expireTime = new Date(now.getTime() + oauth2Yml.getExpire() * 1000);

		//判断是否生成过token
		SysUserTokenEntity tokenEntity = this.getOne(new LambdaQueryWrapper<SysUserTokenEntity>()
				.eq(SysUserTokenEntity::getUserId, userId)
				.eq(SysUserTokenEntity::getIdentity, identity)
		);

		if(tokenEntity == null){
			tokenEntity = new SysUserTokenEntity();
			tokenEntity.setUserId(userId);
			tokenEntity.setToken(token);
			tokenEntity.setUpdateTime(now);
			tokenEntity.setExpireTime(expireTime);
			tokenEntity.setIdentity(identity);
			//保存token
			this.save(tokenEntity);
		}else{
			tokenEntity.setToken(token);
			tokenEntity.setUpdateTime(now);
			tokenEntity.setExpireTime(expireTime);
			tokenEntity.setIdentity(identity);
			//更新token
			this.update(new LambdaUpdateWrapper<SysUserTokenEntity>()
					.eq(SysUserTokenEntity::getUserId, userId)
					.eq(SysUserTokenEntity::getIdentity, identity)
					.set(SysUserTokenEntity::getToken, token)
					.set(SysUserTokenEntity::getUpdateTime, now)
					.set(SysUserTokenEntity::getExpireTime, expireTime)
			);
		}

		R r = R.ok().put(oauth2Yml.getHeader(), token).put("expire", oauth2Yml.getExpire());
		return r;
	}




	@Override
	public void logout(long userId) {
		//生成一个token
		String token = TokenGenerator.generateValue();

		//修改token
		this.update(new LambdaUpdateWrapper<SysUserTokenEntity>()
				.eq(SysUserTokenEntity::getUserId, userId)
				.isNull(SysUserTokenEntity::getIdentity)
				.set(SysUserTokenEntity::getToken, token));
	}
}
